import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_yn_goods/config/app_translations.dart';
import 'package:flutter_yn_goods/utils/loading/loading_utils.dart';
import 'package:get/get.dart';


class DebounceGestureDetector extends StatefulWidget {
  final VoidCallback? onTap;
  final Future Function()? asyncTap;
  final Widget child;
  final Duration debounceDuration;
  final bool useInkWell; //inkWell可以在子控件有空隙的情况下保证点击范围
  final bool showToast;

  const DebounceGestureDetector({
    Key? key,
    this.onTap,
    this.asyncTap,
    required this.child,
    this.useInkWell = false,
    this.debounceDuration = const Duration(milliseconds: 250),
    this.showToast = false,
  }) : super(key: key);

  @override
  State<DebounceGestureDetector> createState() => _DebounceGestureDetectorState();
}

class _DebounceGestureDetectorState extends State<DebounceGestureDetector> {
  bool _isTapped = false;

  @override
  Widget build(BuildContext context) {
    return widget.useInkWell
        ? InkWell(
            onTap: _onTap,
            child: widget.child,
          )
        : GestureDetector(
            onTap: _onTap,
            // behavior: HitTestBehavior.opaque,
            child: widget.child,
          );
  }

  _onTap() async {
    var viewInsets = MediaQuery.of(context).viewInsets.bottom;
    FocusManager.instance.primaryFocus?.unfocus();
    if (viewInsets > 0) {
      await Future.delayed(const Duration(milliseconds: 250), () => true);
    }
    if (!_isTapped) {
      _isTapped = true;
      if (widget.onTap != null) {
        _tapWithOnTap();
      } else if (widget.asyncTap != null) {
        _tapWithAsyncTap();
      }
    } else {
      if (widget.showToast && !LoadingUtils.isShow) {
        LoadingUtils.showToast(LocaleKeys.too_many_operations.tr);
      }
    }
  }

  void _tapWithAsyncTap() async {
    await widget.asyncTap?.call();
    if (mounted) {
      setState(() {
        _isTapped = false;
      });
    }
  }

  void _tapWithOnTap() {
    widget.onTap?.call();
    Future.delayed(widget.debounceDuration, () {
      if (mounted) {
        setState(() {
          _isTapped = false;
        });
      }
    });
  }
}

class ThrottleGestureDetector extends StatefulWidget {
  final VoidCallback? onTap;
  final Widget child;
  final Duration debounceDuration;
  final bool useInkWell; //inkWell可以在子控件有空隙的情况下保证点击范围

  const ThrottleGestureDetector({
    Key? key,
    this.onTap,
    required this.child,
    this.useInkWell = false,
    this.debounceDuration = const Duration(milliseconds: 250),
  }) : super(key: key);

  @override
  State<ThrottleGestureDetector> createState() => _ThrottleGestureDetectorState();
}

class _ThrottleGestureDetectorState extends State<ThrottleGestureDetector> {
  Timer? _timer;

  @override
  Widget build(BuildContext context) {
    return widget.useInkWell
        ? InkWell(
            onTap: _onTap,
            child: widget.child,
          )
        : GestureDetector(
            onTap: _onTap,
            child: widget.child,
          );
  }

  void _onTap() {
    FocusManager.instance.primaryFocus?.unfocus();
    if (_timer != null && _timer!.isActive) {
      _timer!.cancel();
    }
    _timer = Timer(widget.debounceDuration, () {
      widget.onTap?.call();
    });
  }

  @override
  void dispose() {
    if (_timer != null && _timer!.isActive) {
      _timer!.cancel();
    }
    _timer = null;
    super.dispose();
  }
}
